package com.csi.controller.back;

import cn.hutool.jwt.JWTUtil;
import com.csi.domain.Result;
import com.csi.domain.User;
import com.csi.domain.UserAssess;
import com.csi.service.TokenService;
import com.csi.service.UserService;
import com.csi.utils.ReturnResults;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
/**
 * 个人中心信息管理
 */
public class UserInfoController {
    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 用户登录
     * @param user 用户
     * @return
     */
    @PostMapping(value = "/login")
    public Object login(User user) {
        User users = userService.login(user.getUserNickname(), user.getUserPassword());
        if (users != null) {
            String token = tokenService.UserLoginToken(users.getId(),users.getUserNickname());
            return new Result<>("登陆成功！", ReturnResults.ACCESS_SUCCESS, token);
        } else {
            return new Result<>("登陆失败！", ReturnResults.ACCESS_FAILED);
        }
    }

    /**
     * 用户注册
     * @param user 用户对象
     * @return
     */
    @PostMapping(value = "/register")
    public Object register( User user) {
        int ret = userService.register(user);
        if (ret > 0) {
            return new Result<>("注册成功！", ReturnResults.ACCESS_SUCCESS);
        } else {
            return new Result<>("注册失败！", ReturnResults.ACCESS_FAILED);
        }
    }

    /**
     * 根据用户id查找用户信息
     *
     * @param  request
     * @return 用户信息
     */
    @GetMapping(value = "/showAllInfo")
    public Result showAllInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("userId").toString());
        User user = userService.findById(userId);
        if (user != null) {
            return new Result<>("查找成功", ReturnResults.ACCESS_SUCCESS, user);
        }
        return new Result("暂无用户信息", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据id修改用户信息
     * @param user 用户信息
     * @return 执行条数
     */
    @PostMapping(value = "/updateUser", produces = "application/json;charset=utf-8")
    public Object updateUser( User user) {
        int updated = userService.updateUser(user);
        if (updated > 0) {
            System.out.println(updated);
            return new Result<User>("更新用户信息成功！", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("用户信息更新失败！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 用户修改密码
     * @param id 用户id
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return 执行结果
     */
    @PostMapping("/changePassword")
    @ResponseBody
    public String changePassword(long id, String oldPassword,  String newPassword) {
        int result = userService.changePassword(id, oldPassword, newPassword);
        if (result == 1) {
            return "密码修改成功！";
        }else{
            return "原密码错误，密码修改失败！";
        }
    }

    /**
     * 根据用户id查找用户的评价
     * @param request 用户id
     * @return 执行结果
     */
    @GetMapping("/getUserAssessByUserId")
    public Object getUserAssessByUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("userId").toString());
        List<UserAssess> userAssessList = userService.getUserAssessByUserId(userId);
        if (userAssessList != null) {
            return new Result("查找成功", ReturnResults.ACCESS_SUCCESS, userAssessList);
        }
        return new Result("暂无评价！", ReturnResults.ACCESS_FAILED);
    }

    @GetMapping("/showUser")
    public Object getUserByHotelId(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("hotelId").toString());
        List<User>users = userService.getUsersByHotelId(hotelId);
        if (users != null) {
            return new Result("查找成功", ReturnResults.ACCESS_SUCCESS, users);
        }
        return new Result("查找失败！", ReturnResults.ACCESS_FAILED);
    }
}
